package com.gc.repository.impl;

import com.gc.entity.Ccs;
import com.gc.entity.Check;
import com.gc.entity.Student;
import com.gc.entity.TeachClass;
import com.gc.repository.CcsRepository;
import com.gc.util.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 教学班与学生对应表
 * (corresponding teachclass and student)
 */
public class CcsRepositoryImpl implements CcsRepository {

    @Override
    public List<Ccs> findByClassId(String classid) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<Ccs> query = qr.query("select * from  ccs WHERE classid = ?", new BeanListHandler<>(Ccs.class), classid);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Ccs> findByStudentId(String sid) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<Ccs> query = qr.query("select * from ccs WHERE sid = ?", new BeanListHandler<>(Ccs.class), sid);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean Add(Ccs ccs) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            qr.update("insert into ccs(classid,sid) VALUES (?, ?)",
                ccs.getClassid(),
                ccs.getSid()
            );
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean DelByStudentId(String sid) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("delete from ccs WHERE sid = ?", sid);
            System.out.println(res);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean DelByClassId(String classid) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("delete from ccs WHERE classid = ?", classid);
            System.out.println(res);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
